package com.yinhe.module.dr;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;
import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;
import com.yinhe.system.domain.YhContract;
import com.yinhe.system.domain.YhSealInfo;
import com.yinhe.system.service.IYhSealInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 印章基础信息Controller
 *
 * @author dr
 * @date 2022-07-14
 */
@RestController
@RequestMapping("/dr/info")
public class DrSealInfoController extends BaseController
{
    @Autowired
    private IYhSealInfoService yhSealInfoService;

    /**
     * 查询印章基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('dr:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhSealInfo yhSealInfo)
    {
        startPage();
        List<YhSealInfo> list = yhSealInfoService.selectYhSealInfoList(yhSealInfo);
        return getDataTable(list);
    }

    @GetMapping("/list1")
    public TableDataInfo list1(YhSealInfo yhSealInfo)
    {
        List<YhSealInfo> list = yhSealInfoService.selectYhSealInfoList(yhSealInfo);
        return getDataTable(list);
    }
    /**
     * 导出印章基础信息列表
     */
    @PreAuthorize("@ss.hasPermi('dr:info:export')")
    @Log(title = "印章基础信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhSealInfo yhSealInfo)
    {
        List<YhSealInfo> list = yhSealInfoService.selectYhSealInfoList(yhSealInfo);
        ExcelUtil<YhSealInfo> util = new ExcelUtil<YhSealInfo>(YhSealInfo.class);
        util.exportExcel(response, list, "印章基础信息数据");
    }

    /**
     * 获取印章基础信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('dr:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhSealInfoService.selectYhSealInfoById(id));
    }

    /**
     * 新增印章基础信息
     */
    @PreAuthorize("@ss.hasPermi('dr:info:add')")
    @Log(title = "印章基础信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhSealInfo yhSealInfo)
    {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhSealInfo.setUserId(sysUser.getUserId());
        yhSealInfo.setDeptId(sysUser.getDeptId());
        return toAjax(yhSealInfoService.insertYhSealInfo(yhSealInfo));
    }

    /**
     * 修改印章基础信息
     */
    @PreAuthorize("@ss.hasPermi('dr:info:edit')")
    @Log(title = "印章基础信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhSealInfo yhSealInfo)
    {
        return toAjax(yhSealInfoService.updateYhSealInfo(yhSealInfo));
    }

    /**
     * 删除印章基础信息
     */
    @PreAuthorize("@ss.hasPermi('dr:info:remove')")
    @Log(title = "印章基础信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhSealInfoService.deleteYhSealInfoByIds(ids));
    }
    /**
     * 导入印章基础信息
     */
    @Log(title = "印章信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('dr:info:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<YhSealInfo> util = new ExcelUtil<YhSealInfo>(YhSealInfo.class);
        List<YhSealInfo> sealInfoList = util.importExcel(file.getInputStream());
        String message = yhSealInfoService.importSealinfo(sealInfoList, updateSupport);
        return AjaxResult.success(message);
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<YhSealInfo> util = new ExcelUtil<YhSealInfo>(YhSealInfo.class);
        util.importTemplateExcel(response, "印章基础信息");
    }


}
